package com.sgb.kjwl.viewmodel.transport;

import static com.mob.tools.utils.DeviceHelper.getApplication;

import android.text.TextUtils;

import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.transport.VehicleDetail;
import com.sgb.kjwl.view.ui.activity.vehicleManagement.VehicleDetailActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.util.UserOperating;
import com.swgk.core.view.entity.PermissionEntity;

import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class VehicleDetailModel extends BaseViewModel {
    public MutableLiveData<Boolean> ldInquiry = new MutableLiveData<>();//询价权限
    public MutableLiveData<String> statusLiveData = new MutableLiveData<>();
    private VehicleDetailActivity activity;

    public VehicleDetailModel(VehicleDetailActivity activity) {
        this.activity = activity;

    }


    public void loadData(VehicleDetailActivity activity, String driverAuthNo) {
        DialogHelper.showProgressDialog(activity, "数据加载中...");

        NewAPIManager.getInstance().getLogisticsAPI().getFindCarVehicleDetail(driverAuthNo).enqueue(new Callback<BaseEntity<VehicleDetail>>() {
            @Override
            public void onResponse(Call<BaseEntity<VehicleDetail>> call, Response<BaseEntity<VehicleDetail>> response) {
                DialogHelper.dismissProgressDialog();
                if(response.body()==null){
                    return;
                }
                if(response.body().getState().equals("fail")){
                    if(!TextUtils.isEmpty(response.body().getMsg())){
                        MToast.showToast(getApplication(), response.body().getMsg());
                    }else {
                        MToast.showToast(getApplication(), response.body().getState());
                    }
                    return;
                }
                if(response.body().getData()==null){
                    return;
                }

                if (response.body().getCode().equals("200")) {
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            activity.refreshData(response.body().getData());
                        }
                    });
                } else {
                    MToast.showToast(getApplication(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<VehicleDetail>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    /**
     * 通话记录
     */
//    public void callRecord(String driverAuthNo, String phone) {
//
//        JsonObject json = new JsonObject();
//        json.addProperty("driverAuthNo", driverAuthNo);
//        json.addProperty("method", 4);
//        json.addProperty("phone", phone);
//        RequestBody body = RequestBody.create(json.toString(), MediaType.parse("application/json"));
//        APIManager.getInstance().getUserAPI().callSend(body).enqueue(new CommonCallback<BaseEntity>() {
//            @Override
//            protected void response(BaseEntity data) {
//            }
//
//            @Override
//            protected void failure(Throwable t) {
//            }
//        });
//    }
    public void refresh(VehicleDetailActivity mActivity) {
        //获取用户数据
        NewAPIManager.getInstance().getLogisticsAPI().getLogisticsPermissions().enqueue(new ICallBack<BaseEntity<PermissionEntity>>() {
            @Override
            public void onSuccess(BaseEntity<PermissionEntity> data) {
                if(data.isSuccess()){
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COM_TYPE, data.getData().getUserType());
                    UserOperating.getInstance().setUserInfo(data.getData());
                    mActivity.setUserInfo();
                }
            }
        });
    }

    /**
     * 查询询价权限
     * @param carNo
     */
    public void getInquiryPermissions(String carNo) {
        if (!TextUtils.isEmpty(carNo)) {
            NewAPIManager.getInstance().getLogisticsAPI().getInquiryPermissions(carNo, "").enqueue(new ICallBack<BaseEntity<Boolean>>() {

                @Override
                public void onSuccess(BaseEntity<Boolean> data) {
                    if(data.isSuccess()){
                        ldInquiry.postValue(data.getData());
                    }
                }
            });
        }
    }

    public void getProcessStatus(String companyNo, String approveType) {
        NewAPIManager.getInstance().getMaterialAPI().getProcessStatus(companyNo, approveType).enqueue(new ICallBack<BaseEntity<Object>>() {
            @Override
            public void onSuccess(BaseEntity<Object> data) {
                // "status": "1" //1 表示有审批 0 代表无审批
                String status = ((Map<String, String>) data.getData()).get("status");
                if (!TextUtils.isEmpty(status)) {
                    statusLiveData.postValue(status);
                }
            }
        });
    }


}
